﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agr.CQRS.Domain.Commands;
using Agr.CQRS.Infrastructure.ServiceBus;
using log4net;

namespace Agr.CQRS.Infrastructure.Commands
{
    public class CommandIssuer
    {
        IServiceBus servicebus;
        private static ILog Logger = LogManager.GetLogger(typeof(CommandIssuer));

        public CommandIssuer(IServiceBus sb)
        {
            servicebus = sb;
        }

        public void IssueCommand(Command cmd)
        {
            Logger.InfoFormat("Publishing command {0}", cmd.ToString());

            //publishing a command MUST always be done as Command
            //and not as its concrete class. That's because the 
            //command router only listens to Command's and not to 
            //concrete types. This is a limitation in the current implementation
            //of the servicebus.
            servicebus.Publish(cmd);
        }
    }
}
